package com.starbaba.user.datainput.view;

import com.google.gson.JsonObject;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.user.datainput.model.UserDataInputModel;
import com.starbaba.user.datainput.service.UserDataInputService;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings({"rawtypes", "unchecked", "serial"})
public class UserDataInputAction extends BaseAction implements ModelDriven<UserDataInputModel>{

	private UserDataInputModel model;
	
	private UserDataInputService service;
	
	private String savePath;

	private String uploadFileName;

	private File upload;

	private String uploadContentType;
	
	private InputStream inputStream;

	private String fileName;
	
	public String showPage() {
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		try {
			service.showPage(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ActionSupport.SUCCESS;
		
	}
	
	public void queryList() {
		
		HttpSession session = ServletActionContext.getRequest().getSession();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		parsePageRequest();
		PageResponse pageResponse = new PageResponse();
		int start = pageRequest.getIDisplayStart();
		int length = pageRequest.getIDisplayLength();

		try {
			List<UserDataInputModel> list = new ArrayList<UserDataInputModel>();
			list = service.getList(start, length, pageResponse, paramsMap, session);

			pageResponse.setAaData(list);
			pageResponse.setSEcho(pageRequest.getSEcho());

			sendJsonMessage(JsonUtils.toJson(pageResponse, null, true, null, false));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public String uploadFileToTemp() {

		HttpSession session = ServletActionContext.getRequest().getSession();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String operator = getUserVO().getUsername();
		paramsMap.put("operator", operator);
		File file = upload;
		try {
			service.uploadFileToTemp(file, paramsMap, session);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ActionSupport.SUCCESS;
		
	}
	
	public void uploadFileToDB() {
		
		HttpSession session = ServletActionContext.getRequest().getSession();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		String operator = getUserVO().getUsername();
		paramsMap.put("operator", operator);
		try {
			service.uploadRecordToDB(paramsMap, session);
			String message = "上传数据库成功";
			JsonObject object = new JsonObject();
			object.addProperty("message", message);
			sendJsonMessage(object.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void cleanBuf() {
		
		HttpSession session = ServletActionContext.getRequest().getSession();
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(), paramsMap);
		try {
			service.cleanBuf(paramsMap, session);
			String message = "清除缓存成功";
			JsonObject object = new JsonObject();
			object.addProperty("message", message);
			sendJsonMessage(object.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	@Autowired
	@Qualifier("userDataInputService")
	public void setSerivce(UserDataInputService service) {
		this.service = service;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	@Override
	public UserDataInputModel getModel() {
		if (model == null) {
			model = new UserDataInputModel();
		}
		return model;
	}

}
